# TODO: Add comment
# 
# Author: Forever
###############################################################################
library(raster)
library(base)
library(gstat)
library(sqldf)
for (j in 12:8) {
	mon=j
	rasterFile=raster("C:/mavung.tif")
	avg= read.csv("C:/in/AvgTempPress.csv") 
	
	tempFile=rasterFile
	rhFile=rasterFile
	preFile=rasterFile
	
	for (i in 1:length(tempFile)) {
		if(tempFile[i]!=-9999){
			area=tempFile[i]
			tempFile[i]=avg$avg_temp[avg$area_id==area&avg$month==mon]
			rhFile[i]=avg$avg_rh[avg$area_id==area&avg$month==mon]
			preFile[i]=avg$avg_preci_24[avg$area_id==area&avg$month==mon]
			
		}
		
	}
	
	outFile=paste("C:/MetTiff/temp",mon,".tif",sep="")
	writeRaster(tempFile,filename=outFile,format="GTiff")
	
	outFile=paste("C:/MetTiff/rh",mon,".tif",sep="")
	writeRaster(rhFile,filename=outFile,format="GTiff")
	
	outFile=paste("C:/MetTiff/preci",mon,".tif",sep="")
	writeRaster(preFile,filename=outFile,format="GTiff")
	print(mon)
}
print("Done")


